/*
 * @Description: 字典数据
 * @Author: huacong
 * @Date: 2020-11-10 21:34:34
 * @LastEditTime: 2020-11-10 21:43:12
 * @LastEditors: huacong
 */
import { mapGetters, mapActions } from 'vuex'

const pending = {}

const types = {
  mp4: 'video',
  avi: 'video',
  wav: 'video',
  video: 'video',
  mp3: 'audio',
  png: 'image',
  gif: 'image',
  jpg: 'image',
  zip: 'zip',
  rar: 'zip'
}

export default {
  computed: {
    ...mapGetters(['dict', 'user', 'menuList'])
  },
  methods: {
    ...mapActions({
      'getDict': 'dict/getDict'
    }),
    hasRole (list) {
      const { roles } = this.user
      if (typeof list === 'string') {
        list = Array.of(list)
      }
      return roles.some(x => list.includes(x.roleCode))
    },
    dictFilter (val, feild, key = 'value', labelKey = 'label') {
      if (!this.dict[feild] && !pending[feild]) {
        pending[feild] = true
        this.getDict({ fieldName: feild }).then(res => {
          console.log(res)
          const item = res.find(x => String(x[key]) === String(val))
          return item ? item[labelKey] : ''
        }).finally(() => {
          pending[feild] = false
        })
      } else {
        const options = (this.dict[feild] || [])
        const item = options.find(x => String(x[key]) === String(val))
        return item ? item[labelKey] : val
      }
    },
    // 获取按钮权限
    getBtnAuth (key) {
      const { menuList } = this
      return menuList.some(x => x[key] === key)
    },
    resTypeFilter (url) {
      const dotSplit = url?.split('.') || []
      return types[dotSplit[dotSplit.length - 1]] || ''
    }
  }
}
